#include <kuai/misc/sqlite3.hpp>
#include <kuai/misc/job.hpp>

#ifndef _KUAI_DBTOOL_
#define _KUAI_DBTOOL_

namespace kuai {

	class DatabaseJob
		: public ParameterJob
	{
	public:
		DatabaseJob(int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
		virtual void doJob(std::ostream& out_log, std::ostream& err_log, StringMap& par);
		virtual void doJob(Sqlite3DatabasePointer pdb, std::ostream& out_log, std::ostream& err_log, StringMap& par) = 0;


		void dumpTable(const String& table, std::ostream& stream, const String& sep = "|");
		void dumpTable(Sqlite3Statement& table, std::ostream& stream, const String& sep = "|");
		void dumpTable(const String& table, const FileName& file, const String& sep = "|");
		void dumpTable(Sqlite3Statement& table, const FileName& file, const String& sep = "|");


		static void help(std::ostream& output);
	protected:
		Sqlite3DatabasePointer pdb;
		int open_mode;
	};

	class DumpTableJob
		: public DatabaseJob
	{
	public:
		DECLARE_JOB_CREATOR(DumpTableJob);

		DumpTableJob();

		virtual void doJob(Sqlite3DatabasePointer pdb, std::ostream& out_log, std::ostream& err_log, StringMap& par);
	};


}

#endif
